package com.scwatch.core.web.back.f7;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scwatch.common.util.Dates;
import com.scwatch.common.web.Servlets;
import com.scwatch.core.constant.Constants;
import com.scwatch.core.support.Context;
import com.scwatch.ext.domain.Vote;
import com.scwatch.ext.service.VoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by rh005 on 2017/7/7.
 */
@Controller
@RequestMapping("/ext/vote")
public class VoteF7Controller {
    @RequestMapping("choose_vote_multi.do")
    public String f7NodeTreeMulti(Integer[] ids, HttpServletRequest request, HttpServletResponse response, org.springframework.ui.Model modelMap) {
        Integer siteId = Context.getCurrentSiteId();
        List<Vote> list = service.findAll(siteId, null, new PageRequest(0, 2000, Sort.Direction.ASC, "id")).getContent();
        List<Vote> beans = new ArrayList<Vote>();
        if (ids != null) {
            for (Integer id : ids) {
                beans.add(service.get(id));
            }
        }

        modelMap.addAttribute("ids", ids);
        modelMap.addAttribute("beans", beans);
        modelMap.addAttribute("list", list);
        Servlets.setNoCacheHeader(response);
        return "ext/vote/choose_vote_multi";
    }

    @RequestMapping("infochooseVote.do")
    public String infochooseVote(HttpServletRequest request, HttpServletResponse response,
                                 org.springframework.ui.Model modelMap) throws IOException {
        return "ext/vote/infochoose_vote";
    }

    @RequestMapping("getInfoVotePage.do")
    @ResponseBody
    public Object getInfoVotePage(HttpServletRequest request, HttpServletResponse response,
                                  @PageableDefault(sort = {"createDate"}, direction = Sort.Direction.DESC) Pageable pageable,
                                  String title,String startTime,String endTime) throws IOException {
        Integer siteId = Context.getCurrentSiteId();
        Map<String, String[]> params = Servlets.getParamValuesMap(request, Constants.SEARCH_PREFIX);
        params.put("CONTAIN_title",new String[]{title});
//        params.put("GTE_creationDate_Date",new String[]{startTime});
//        params.put("LTE_creationDate_Date",new String[]{endTime});
        params.put("EQ_status",new String[]{"0"});

        service.findAll(siteId,params,pageable);
        Page<Vote> pagelist=service.findAll(siteId,params,pageable);;
//        String a= JSON.toJSONString(pagelist, SerializerFeature.DisableCircularReferenceDetect);
        List<Vote> list=pagelist.getContent();
        JSONArray jsonArray=new JSONArray();
        if(list!=null && list.size()>0){
            JSONObject jsonObject=null;
            for(Vote vote:list){
                jsonObject =new JSONObject();
                jsonObject.put("id",vote.getId());
                jsonObject.put("title",vote.getTitle()==null?"":vote.getTitle());
                String beginTimeStr=Dates.formatDate(vote.getBeginDate(),"yyyy-MM-dd HH:mm:ss");
                String endTimeStr=Dates.formatDate(vote.getEndDate(),"yyyy-MM-dd HH:mm:ss");
                jsonObject.put("beginTime",beginTimeStr==null?"":beginTimeStr);
                jsonObject.put("endTime",endTimeStr==null?"":endTimeStr);
                jsonObject.put("total",vote.getTotal());
                jsonObject.put("type",vote.getType()==0?"实名投票":"匿名投票");
                jsonArray.add(jsonObject);
            }
        }
        JSONObject o=new JSONObject();
        o.put("totalElements",pagelist.getTotalElements());
        o.put("totalPages",pagelist.getTotalPages());
        o.put("content",jsonArray);
        return o;
    }

    @Autowired
    private VoteService service;
}
